import { defineStore } from "pinia";
import loginApi, { type LoginModel } from "@/api/login";
const resetUserInfo = () => ({
    headImg: "",
    id: 0,
    name: "",
    nickName: "",
    loginName: "",
});
const useLoginStore = defineStore("login", {
    state: () => {
        return {
            access_token_zww_2: "",
            userInfo: resetUserInfo(),
            toPath:''
        };
    },
    actions: {
        //登录
        async login(data: LoginModel) {
            try {
                let result = await loginApi.reqLogin(data);
                //由于使用vue2前台项目的接口拿到的 token 是过期的 不能使用  所以这里写死
                // this.access_token_zww_2='30b129e1-45d4-4b64-a522-612499b3316b'
                // localStorage.setItem('ACCESS_TOKEN_ZWW_2','30b129e1-45d4-4b64-a522-612499b3316b')
                this.access_token_zww_2 = result.token;
                localStorage.setItem("ACCESS_TOKEN_ZWW_2", result.token);
            } catch (error) {
                return Promise.reject(error);
            }
        },
        //获取用户信息
        async getUserInfo() {
            try {
                let result = await loginApi.getUserInfo();
                this.userInfo = result;
            } catch (error) {
                return Promise.reject(error);
            }
        },
        //重置用户信息
        async reset() {
            try {
                await loginApi.logout();
                this.access_token_zww_2 = "";
                localStorage.removeItem('ACCESS_TOKEN_ZWW_2')
                this.userInfo = resetUserInfo();
            } catch (error) {
                return Promise.reject(error);
            }
        },
    },
    getters: {},
});
export default useLoginStore;
